package com.project.dynamicloadbean;

import org.springframework.beans.factory.support.BeanDefinitionBuilder;
import org.springframework.beans.factory.support.DefaultListableBeanFactory;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;

import com.project.dynamicloadbean.beans.ProjectWorkChain;
import com.project.dynamicloadbean.config.AppConfig;

public class TryDynamicLoadBean {
	
	public static void main(String[] args) {
		AnnotationConfigApplicationContext ctx = new AnnotationConfigApplicationContext(AppConfig.class);
		DefaultListableBeanFactory beanFactory = (DefaultListableBeanFactory) ctx.getAutowireCapableBeanFactory();
		
		BeanDefinitionBuilder beanDefinitionBuilder = BeanDefinitionBuilder.genericBeanDefinition(ProjectWorkChain.class);
		beanDefinitionBuilder.addPropertyValue("sex", "male");
		
		beanFactory.registerBeanDefinition("projectWorkChain", beanDefinitionBuilder.getBeanDefinition());
		
		ProjectWorkChain bean = ctx.getBean(ProjectWorkChain.class);
		System.out.printf("dynamic load bean info: %s.\n", bean.toString());
		
		System.out.printf("prepare remove bean info>>>>>>\n");
		
		beanFactory.removeBeanDefinition("projectWorkChain");
		
		System.out.println("test for bean: " + ctx.getBean(ProjectWorkChain.class));
		
		ctx.close();
	}
}
